package com.yzt.zhuju.gift.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.yzt.zhuju.R;
import com.yzt.zhuju.gift.GiftManager;
import com.yzt.zhuju.gift.GiftSelectManager;
import com.yzt.zhuju.network.entity.gift.Gift;
import com.yzt.zhuju.network.entity.gift.GiftCacheInfo;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;

/**
 * author : bob
 * date   : 2022/12/5
 * desc   : 礼物的适配器
 */
@SuppressLint("SetTextI18n")
public class GiftItemAdapter extends RecyclerView.Adapter<GiftItemAdapter.ItemViewHolder> {

    private final Context context;
    private List<Gift> list;
    private final int type;
    private final int comboTime = 1;

    public GiftItemAdapter(Context context,  int type) {
        this.context = context;
        this.type = type;
        this.list = new ArrayList<>();
    }

    public void initData(List<Gift> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.list_flower_grid_item, viewGroup, false));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        Gift gift = list.get(position);
        ImageLoadUtil.loadIv(holder.icon.getContext(), gift.getGiftIcon(), holder.icon);
        holder.giftName.setText(gift.getGiftName());

        holder.giftWorth.setText(gift.getCandyCoin() + " 糖币");

        if (gift.isChoosed()) {
            holder.itemGift.setBackgroundResource(R.mipmap.bg_diamond_gift_selected);
        } else {
            holder.itemGift.setBackgroundColor(Color.TRANSPARENT);
        }

        holder.itemGift.setOnClickListener(v -> {
            GiftManager.getInstance().setGiftCacheInfo(new GiftCacheInfo( type, gift));
            gift.setComboTime(comboTime);
            GiftSelectManager.getInstance().selectGiftData( type, gift);
            int[] selectTab = GiftManager.getInstance().getSelectTab();
            int[] selectIndex = new int[4];
            System.arraycopy(selectTab, 0, selectIndex, 0, 3);
            selectIndex[3] = gift.getGiftId();
            GiftManager.getInstance().setSelectIndex(selectIndex);
        });
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final View itemGift;

        private final ImageView icon;

        private final TextView giftName;

        private final TextView giftWorth;

        public ItemViewHolder(View itemView) {
            super(itemView);
            itemGift = itemView.findViewById(R.id.gift_item);
            icon = itemView.findViewById(R.id.gift_item_image);
            giftName = itemView.findViewById(R.id.gift_item_name);
            giftWorth = itemView.findViewById(R.id.gift_item_umoney);
        }
    }
}
